package com.lzx.iteam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.*;
import com.lzx.iteam.adapter.AttendanceRankAdapter;
import com.lzx.iteam.bean.AttendanceRankData;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.GetAttendanceRankMsg;
import com.lzx.iteam.net.LocalLogin;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

import static com.baidu.location.b.g.G;
import static com.baidu.location.b.g.V;


/**
 * Created by Megan on 16/7/22.
 */
public class AttendanceRankActivity extends Activity implements View.OnClickListener {

    private LinearLayout mLlBack, mLlRankName, mLlFilter;
    private RelativeLayout mRlAverageFilter,mRlTotalFilter;
    private ImageView mIvRule,mIvAverageSelected,mIvTotalSelected;
    private TextView mTvTimeName;
    private TextView mTvFilter;
    private TextView mTvTitle;
    private ListView mLvRankList;
    private ImageView mIvRules;
    private String mGroupId;
    private ArrayList<AttendanceRankData> ranks;
    private AttendanceRankAdapter adapter;
    private final int GET_RANK_MSG = 1001;
    private int filterFlag=0;//0:平均时长  1:总时长
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case GET_RANK_MSG:
                    if (msg.arg1 == 0) {
                        ranks = (ArrayList<AttendanceRankData>) msg.obj;
                        adapter.bindData(ranks);


                    } else {
                        if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                            Toast.makeText(AttendanceRankActivity.this, R.string.active_failure_no_network,
                                    Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(AttendanceRankActivity.this, (String) msg.obj,
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.attendance_rank_layout);
        initViews();
        getAttendanceRankMsg();
    }

    private void initViews() {
        mLlBack = (LinearLayout) findViewById(R.id.ll_attendance_rank_back);
        mLlRankName = (LinearLayout) findViewById(R.id.ll_attendance_rank_name);
        mLlFilter = (LinearLayout) findViewById(R.id.ll_attendance_rank_time_filter);
        mRlAverageFilter = (RelativeLayout) findViewById(R.id.rl_attendance_rank_average_time_filter);
        mRlTotalFilter = (RelativeLayout) findViewById(R.id.rl_attendance_rank_total_time_filter);
        mIvRule = (ImageView) findViewById(R.id.iv_attendance_rank_rule);
        mIvAverageSelected = (ImageView) findViewById(R.id.iv_attendance_rank_average_time_img);
        mIvTotalSelected = (ImageView) findViewById(R.id.iv_attendance_rank_total_time_img);
        mTvFilter = (TextView) findViewById(R.id.tv_attendance_rank_filter);
        mTvTimeName = (TextView) findViewById(R.id.tv_attendance_rank_time_name);
        mTvTitle = (TextView) findViewById(R.id.tv_attendance_rank_title);
        mLvRankList = (ListView) findViewById(R.id.lv_attendance_rank_list);
        mIvRules = (ImageView) findViewById(R.id.iv_attendance_rank_rules);

        mLlBack.setOnClickListener(this);
        mLlFilter.setOnClickListener(this);
        mIvRule.setOnClickListener(this);
        mTvFilter.setOnClickListener(this);
        mRlAverageFilter.setOnClickListener(this);
        mRlTotalFilter.setOnClickListener(this);

        adapter = new AttendanceRankAdapter(this);
        mLvRankList.setAdapter(adapter);
        Intent intent = getIntent();
        mGroupId = intent.getStringExtra("group_id");
    }

    private void getAttendanceRankMsg() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin.getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        GetAttendanceRankMsg msg = new GetAttendanceRankMsg(mHandler.obtainMessage(GET_RANK_MSG), this,0);
        msg.mParams = nvps;
        msg.mApi = AsynHttpClient.API_EVENT_ATTENDANCE_RANKING;
        AsynHttpClient.getInstance(this).execEventHttp(msg);
    }

    private void getAttendanceTotalRankMsg() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin.getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        GetAttendanceRankMsg msg = new GetAttendanceRankMsg(mHandler.obtainMessage(GET_RANK_MSG), this,1);
        msg.mParams = nvps;
        msg.mApi = AsynHttpClient.API_EVENT_ATTENDANCE_TOTAL_RANKING;
        AsynHttpClient.getInstance(this).execEventHttp(msg);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_attendance_rank_back:
                if (mLvRankList.getVisibility() == View.VISIBLE) {
                    finish();
                } else {
                    mIvRules.setVisibility(View.GONE);
                    mLlRankName.setVisibility(View.VISIBLE);
                    mLvRankList.setVisibility(View.VISIBLE);
                    mIvRule.setVisibility(View.VISIBLE);
                    mTvTitle.setText("勤奋榜");
                }

                break;
            case R.id.iv_attendance_rank_rule:
                mIvRules.setVisibility(View.VISIBLE);
                mLlRankName.setVisibility(View.GONE);
                mLvRankList.setVisibility(View.GONE);
                mIvRule.setVisibility(View.GONE);
                mTvTitle.setText("详细规则");
                break;
            case R.id.tv_attendance_rank_filter:
                mLlFilter.setVisibility(mLlFilter.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                break;
            case R.id.rl_attendance_rank_average_time_filter:
                filterFlag=0;
                updateFilterMsg();
                break;
            case R.id.rl_attendance_rank_total_time_filter:
                filterFlag=1;
                updateFilterMsg();
                break;
            case R.id.ll_attendance_rank_time_filter:
                mLlFilter.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        // super.onBackPressed();
        if (mLvRankList.getVisibility() == View.VISIBLE) {
            finish();
        } else {
            mIvRules.setVisibility(View.GONE);
            mLlRankName.setVisibility(View.VISIBLE);
            mLvRankList.setVisibility(View.VISIBLE);
            mIvRule.setVisibility(View.VISIBLE);
            mTvTitle.setText("勤奋榜");
        }

    }


    private void updateFilterMsg(){
        if(filterFlag==0){
            mIvAverageSelected.setVisibility(View.VISIBLE);
            mIvTotalSelected.setVisibility(View.GONE);
            mTvTimeName.setText("平均时长");
            getAttendanceRankMsg();
        }else{
            mIvTotalSelected.setVisibility(View.VISIBLE);
            mIvAverageSelected.setVisibility(View.GONE);
            mTvTimeName.setText("总时长");
            getAttendanceTotalRankMsg();
        }
        mLlFilter.setVisibility(View.GONE);
    }
}
